<template>
  <el-card v-loading="loading" shadow="never">
    <el-form :model="form" label-width="180px">
      <el-tabs v-model="activeName">
        <el-tab-pane label="系統設置" name="first">
          <el-form-item label="商品庫存預警">
            <el-input-number v-model="form.num_police" :min="1" :max="1000" />
          </el-form-item>
          <el-form-item label="最低充值金額">
            <el-input-number v-model="form.min_money" :min="1" :max="10000" />
          </el-form-item>
          <el-form-item label="最高充值金額">
            <el-input-number v-model="form.max_money" />
          </el-form-item>
          <el-form-item label="用戶協議">
            <Editor v-model="form.agreement" />
          </el-form-item>
        </el-tab-pane>

        <!-- <el-tab-pane label="推送" name="second">
          <el-form-item label="推送">
            <Editor v-model="form.push" />
          </el-form-item>
        </el-tab-pane> -->
      </el-tabs>
      <el-form-item>
        <el-button type="primary" @click="submit">保存</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script setup>
import { reactive, ref } from "vue";
import { getConfig, createConfig, updateConfig } from "~/api/systems.js";
import { toast } from "../../composables/util";
import Editor from "~/components/Editor.vue";
const form = reactive({
  num_police: 1,
  min_money: 1,
  max_money: 1,
  agreement: "",
  push: "",
});
const loading = ref(false);

const activeName = ref("first");
function getData() {
  loading.value = true;
  getConfig()
    .then((res) => {
      for (const k in form) {
        form[k] = res[k];
      }
    })
    .finally((err) => {
      loading.value = false;
    });
}

getData();

const submit = () => {
  loading.value = true;
  updateConfig(1, form)
    .then((res) => {
      toast("修改成功");
    })
    .finally(() => {
      loading.value = false;
    });
};
</script>
